package org.example.stopthreads;

/**
 * 带有sleep的中断写法
 */
public class RightWayStopThreadWithSleep {

  public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(() -> {
      try {
        int num = 0;
        boolean interrupted = Thread.currentThread().isInterrupted();
        System.out.println(interrupted);
        while (num <= 300 && !interrupted) {
          if (num % 100 == 0) {
            System.out.println(num);
          }
          num++;
        }
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    });
    thread.start();
    Thread.sleep(500);
    thread.interrupt();
  }
}
